package com.cat.model.web.action.admin;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alonew.core.commons.page.Page;
import com.cat.model.biz.service.admin.InterviewService;
import com.cat.model.common.model.common.CompanyInfoVO;
import com.cat.model.common.model.common.InterviewVO;
import com.cat.model.common.util.ImageUtil;
import com.cat.model.dao.common.PlmCommDAO;
import com.cat.model.dao.mapper.InterviewMapper;
import com.cat.model.web.action.base.AdminAction;

public class InterviewManagerAction extends AdminAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7824523552294161997L;

	private Long id;
	private File coverImage;
	private String coverImageFileName;
	private InterviewVO interviewVO;
	private Integer type;
	private String name;
	
	@Autowired
	private PlmCommDAO plmCommDAO;
	@Autowired
	private InterviewService interviewService;
	@Autowired
	private InterviewMapper interviewMapper;
	/**
	 * list
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String list() {
		Map<String, Object> condition = new HashMap<String, Object>();
		if(type!=null){
			condition.put("type", type);
			
		}
		if(StringUtils.isNotBlank(name)){
			condition.put("name", name);
		}
		Page page = this.fetchPageParams();
		page.setNumPerPage(20);
		
		Long totalCount = plmCommDAO.searchSize("manager.searchInterviewSize", condition);
		page.setTotalCount(totalCount);
		int start = (page.getPageNum() -1) * page.getNumPerPage();
		if(start < 0){
			start = 0;
		}
		
		List<InterviewVO> list = (List<InterviewVO>)plmCommDAO.search("manager.searchInterview", condition, start, page.getNumPerPage());
		for (InterviewVO interviewVO : list) {
			interviewVO.setCoverImage(ImageUtil.wrapImageUrl(interviewVO.getCoverImage()));
		}
		request.setAttribute("list", list);
		request.setAttribute("size", totalCount);
		return "list";
	}
	
	public String deleteSingle() {
		interviewService.deleteInterview(id);
		okAlert("删除成功！");
		return "list";
	}
	
	public String toAdd(){
		return "add";
	}
	
	public String add(){
		interviewService.createInterview(interviewVO,coverImage,coverImageFileName);
		parentAlert(true, "保存成功");
		return "list";
	}
	
	public String toEdit(){
		interviewVO = interviewMapper.getInterview(id);
		return "edit";
	}
	
	public String edit(){
		interviewService.updateInterview(interviewVO,coverImage,coverImageFileName);
		parentAlert(true, "保存成功");
		return "list";
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public InterviewVO getInterviewVO() {
		return interviewVO;
	}

	public void setInterviewVO(InterviewVO interviewVO) {
		this.interviewVO = interviewVO;
	}

	public File getCoverImage() {
		return coverImage;
	}

	public void setCoverImage(File coverImage) {
		this.coverImage = coverImage;
	}

	public String getCoverImageFileName() {
		return coverImageFileName;
	}

	public void setCoverImageFileName(String coverImageFileName) {
		this.coverImageFileName = coverImageFileName;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	
}
